package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.LessPay;
import com.digital.welcome.entity.User;
import com.digital.welcome.mapper.LessPayMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.LessPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 莲童的雨儿
 */
@Service
public class LessPayServiceImpl extends ServiceImpl<LessPayMapper, LessPay> implements LessPayService {

    @Autowired
    private LessPayMapper lessPayMapper;

    @Override
    public String LessPayDo(LessPay lessPay) {
        if (lessPay != null && lessPay.getUserName() != null && lessPay.getImgUrl() != null && !"".equals(lessPay.getImgUrl())) {
            lessPay.setFlag(0);
            lessPayMapper.insert(lessPay);
            return JSON.toJSONString(new Result<>().setCode(200).setMessage("申请成功"));
        }
        return JSON.toJSONString(new Result<>().setCode(400).setMessage("信息有误"));
    }

    @Override
    public String getFlag(User user) {
        if (user != null && user.getUserName() != null && !"".equals(user.getUserName())) {
            Integer flag = null;
            QueryWrapper<LessPay> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name", user.getUserName());
            LessPay lessPay = lessPayMapper.selectOne(queryWrapper);
            if (lessPay == null) {
                flag = 2;
            } else {
                flag = lessPay.getFlag();
            }
            return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(flag));
        }
        return JSON.toJSONString(new Result<>().setCode(400).setMessage("信息有误"));
    }

    @Override
    public String getList() {

        List<LessPay> lessPayList = null;
        QueryWrapper<LessPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flag", 0);
        lessPayList = lessPayMapper.selectList(queryWrapper);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(lessPayList));
    }

    @Override
    public String verify(LessPay lessPay) {
        if (lessPay != null && lessPay.getUserName() != null && lessPay.getFlag() != null) {
            QueryWrapper<LessPay> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_name", lessPay.getUserName());
            lessPayMapper.update(lessPay, queryWrapper);
            return JSON.toJSONString(new Result<>().setCode(200).setMessage("审核成功"));
        }
        return JSON.toJSONString(new Result<>().setCode(400).setMessage("信息有误"));
    }
}
